package com.hm.client.okclient.controller;

import com.hm.client.okclient.service.OkpokerHallClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class ConsumerController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OkpokerHallClient hallHallClient;

    @GetMapping("/consumer")
    public String dc() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("hall-hall");
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/dc";
        System.out.println(url);
        return restTemplate.getForObject(url, String.class);
    }

    //使用ribbon调用远程接口（接口队列名不能有下划线）
    @GetMapping("/ribbon_consumer")
    @HystrixCommand(fallbackMethod = "fallback")
    public String ribbonDc() {
        return restTemplate.getForObject("http://hall-hall/dc", String.class);
    }

    //使用feign调用远程接口
    @GetMapping("/feign_consumer")
    public String feginDc() {
        return hallHallClient.consumer();
    }

    @PostMapping("/feign_uploadFile")

    public String feignUploadFile(@RequestPart(value = "file") MultipartFile file) {
        return hallHallClient.handleFileUpload(file);
    }

    public String fallback() {
        return "调用方法失败";
    }
}
